package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.global.BuyerConstant;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.JsonUtils;
import com.epfresh.api.widget.CommonInputItem;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.ApplyInfo;
import com.epfresh.supplier.entity.StatusEntity;

public class ApplyInfoActivity extends BaseActivity {


    private CommonInputItem ciAgentCity;
    private CommonInputItem ciAgentMarket;
    private CommonInputItem ciAgentStoreName;
    private CommonInputItem ciAgentConsignee;
    private CommonInputItem ciAgentConsigneePhone;
    private CommonInputItem ciAgentConsigneeAddress;
    private CommonInputItem ciAgentPrincipal;
    private CommonInputItem ciAgentPrincipalPhone;
    private CommonInputItem ciAgentDescribe;
    private CommonInputItem ciAgentMsg;
    private CommonInputItem ciApplyStoreName;
    private TextView btnApply;


    String mBaseStoreId;

    void initView() {
        ciAgentCity = (CommonInputItem) findViewById(R.id.ci_agent_city);
        ciAgentMarket = (CommonInputItem) findViewById(R.id.ci_agent_market);
        ciAgentStoreName = (CommonInputItem) findViewById(R.id.ci_agent_store_name);
        ciAgentConsignee = (CommonInputItem) findViewById(R.id.ci_agent_consignee);
        ciAgentConsigneePhone = (CommonInputItem) findViewById(R.id.ci_agent_consignee_phone);
        ciAgentConsigneeAddress = (CommonInputItem) findViewById(R.id.ci_agent_consignee_address);
        ciAgentPrincipal = (CommonInputItem) findViewById(R.id.ci_agent_principal);
        ciAgentPrincipalPhone = (CommonInputItem) findViewById(R.id.ci_agent_principal_phone);
        ciAgentDescribe = (CommonInputItem) findViewById(R.id.ci_agent_describe);
        ciAgentMsg = (CommonInputItem) findViewById(R.id.ci_agent_msg);
        ciApplyStoreName = (CommonInputItem) findViewById(R.id.ci_apply_base_store_name);

        btnApply = (TextView) findViewById(R.id.tv_btn);

        btnApply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reqApply();
            }
        });
    }


    void updateView(ApplyInfo applyInfo) {

       /* {
            "consigneeName" : "zx",
                "consigneeAddress" : "上海市上海市普陀区测试",
                "marketName" : "三林",
                "consigneeLat" : null,
                "description" : "domfree6哈哈补习班好像",
                "managerName" : "1",
                "consigneeAddressOnMap" : null,
                "managerPhone" : "18621643734",
                "name" : "李小姐的蔬菜水果店",
                "cityName" : "上海市",
                "consigneeLng" : null,
                "consigneePhone" : "12345644444"
        }*/
        if (applyInfo != null) {
            ciApplyStoreName.setContentText(applyInfo.getBaseStoreName());
            ciAgentCity.setContentText(applyInfo.getCityName());
            ciAgentMarket.setContentText(applyInfo.getMarketName());
            ciAgentStoreName.setContentText(applyInfo.getAgentStoreName());

            ciAgentConsignee.setContentText(applyInfo.getConsigneeName());
            ciAgentConsigneePhone.setContentText(applyInfo.getConsigneePhone());
            ciAgentConsigneeAddress.setContentText(applyInfo.getConsigneeAddress());
            String managerName = applyInfo.getManagerName();
            ciAgentPrincipal.setContentText(managerName);
            if (managerName != null && !"".equals(managerName)){
                ciAgentPrincipal.setCanEditNoBackgroudChange(false);
            }else {
                ciAgentPrincipal.setCanEditNoBackgroudChange(true);
            }
            String managerPhone = applyInfo.getManagerPhone();
            if (managerPhone != null && !"".equals(managerPhone)){
                ciAgentPrincipalPhone.setCanEditNoBackgroudChange(false);
            }else {
                ciAgentPrincipalPhone.setCanEditNoBackgroudChange(true);
            }
            ciAgentPrincipalPhone.setContentText(managerPhone);
            ciAgentDescribe.setContentText(applyInfo.getDescription());
            ciAgentMsg.setContentText("");


        }
    }

    //buy/store/agent/applyInfo
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_info);
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);

        toolbarTitle.setText("申请资料");
        if (savedInstanceState != null) {
            getIntent().putExtra("baseStoreId", savedInstanceState.getString("baseStoreId"));
        }

        mBaseStoreId = getIntent().getStringExtra("baseStoreId");

        initView();

        reqApplyInfo();

        if (getIntent().getStringExtra("daibanzhong") != null && getIntent().getStringExtra("daibanzhong").equals("代办中")) {
            btnApply.setVisibility(View.GONE);
            ciAgentConsignee.setCanEdit(false);
            ciAgentConsigneePhone.setCanEdit(false);
            ciAgentMsg.setCanEdit(false);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);


    }


    @Override
    public void onSaveInstanceState(Bundle outState) {

        outState.putString("baseStoreId", getIntent().getStringExtra("baseStoreId"));
        super.onSaveInstanceState(outState);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.v_receive:

                break;
            case R.id.ci_product_name:

                break;


        }
    }


    @Override
    protected void onRestart() {
        super.onRestart();

    }


    void reqApplyInfo() {
        //buy/store/agent/applyInfo

        RequestEntityMapSupply RequestEntityMapSupply = new RequestEntityMapSupply();
        RequestEntityMapSupply.setCmd(BuyerConstant.CMD_BUYER_STORE_AGENT_APPLYINFO);
        RequestEntityMapSupply.putParameter("agentStoreId", BaseApplication.getInstance().getStoreId());
        RequestEntityMapSupply.putParameter("storeId", mBaseStoreId);

        requestBuyer(RequestEntityMapSupply, BuyerConstant.CMD_BUYER_STORE_AGENT_APPLYINFO, new OnRequestListener<ApplyInfo>() {
            @Override
            public ApplyInfo jsonToObj(String responseStr) {
                return JsonUtils.fromJson(responseStr, ApplyInfo.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                showExceptionView(R.mipmap.order_no, "请点击重试", HttpRequest.FAIL_NET_FAULT);
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                showExceptionView(R.mipmap.order_no, "请点击重试", HttpRequest.FAIL_NET_FAULT);
            }

            @Override
            public void onStart(Object tag) {


                showProgressView();
            }

            @Override
            public void onResponse(ResponseEntity<ApplyInfo> response, Object tag) {

                showMainView();

                if (response != null) {
                    updateView(response.getResponseElement());
                }


            }
        });
    }

    void reqApply() {
        //buy/store/agent/applyInfo
       /* {
            baseStoreId: "1",
                    remark: "",
                managerName: "",
                managerPhone: "",
                consigneeProvinceId: "",
                consigneeCityId: "",
                consigneeDistrictId: "",
                consigneeAddress: "",
                consigneeName: "",
                consigneePhone: "",
                consigneeAddressOnMap: "",
                consigneeLat: "",
                consigneeLng: "",
        }*/
        String agentConsignee = ciAgentConsignee.getContentText();

        if (agentConsignee == null || "".equals(agentConsignee)){
            toast("请输入收货人");
            return;
        }
        String agentConsigneePhone = ciAgentConsigneePhone.getContentText();
        if (agentConsigneePhone == null || "".equals(agentConsigneePhone)){
            toast("请输入收货人电话");
            return;
        }

/*        if (agentConsigneePhone.length() != 11){
            toast("请输入正确的收货人电话");
            return;
        }*/

        RequestEntityMapSupply RequestEntityMapSupply = new RequestEntityMapSupply();
        RequestEntityMapSupply.setCmd(BuyerConstant.CMD_BUYER_STORE_AGENT_APPLY);
        RequestEntityMapSupply.putParameter("agentStoreId", BaseApplication.getInstance().getStoreId());
        RequestEntityMapSupply.putParameter("storeId", mBaseStoreId);
        RequestEntityMapSupply.putParameter("remark", ciAgentMsg.getContentText());
        RequestEntityMapSupply.putParameter("managerName", ciAgentPrincipal.getContentText());
        RequestEntityMapSupply.putParameter("managerPhone", ciAgentPrincipalPhone.getContentText());
        RequestEntityMapSupply.putParameter("consigneeProvinceId", null);
        RequestEntityMapSupply.putParameter("consigneeCityId", null);
        RequestEntityMapSupply.putParameter("consigneeDistrictId", null);
        RequestEntityMapSupply.putParameter("consigneeAddress", ciAgentConsigneeAddress.getContentText());
        RequestEntityMapSupply.putParameter("consigneeName", agentConsignee);
        RequestEntityMapSupply.putParameter("consigneePhone", agentConsigneePhone);
        RequestEntityMapSupply.putParameter("consigneeAddressOnMap", null);
        RequestEntityMapSupply.putParameter("consigneeLat", null);
        RequestEntityMapSupply.putParameter("consigneeLng", null);
        RequestEntityMapSupply.putParameter("applicantId", ApplicationHelper.getInstance().getUser().getAccountId());
        RequestEntityMapSupply.putParameter("applicantPhone",ApplicationHelper.getInstance().getUser().getPhone());


        requestBuyer(RequestEntityMapSupply, BuyerConstant.CMD_BUYER_STORE_AGENT_APPLY, new OnRequestListener<ApplyInfo>() {
            @Override
            public ApplyInfo jsonToObj(String responseStr) {
                return JsonUtils.fromJson(responseStr, ApplyInfo.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                hideProgressDialog();
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                hideProgressDialog();
            }

            @Override
            public void onStart(Object tag) {

                showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<ApplyInfo> response, Object tag) {
                hideProgressDialog();

                if (response != null){
                    ApplyInfo responseElement = response.getResponseElement();
                    if (responseElement!=null){

                        setResult(RESULT_OK);
                        finish();

                    }
                }
            }
        });
    }

}